package com.group6.exam.teacher.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group6.exam.common.entity.ExamEntity;
import com.group6.exam.common.entity.PageModel;
import com.group6.exam.teacher.service.ExamServiceImpl;
import com.group6.exam.teacher.service.IExamService;
import com.group6.exam.teacher.service.ITeacherService;
import com.group6.exam.teacher.service.TeacherServiceImpl;

public class ExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ExamServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if ("main".equals(command)) {
			main(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("preUpdate".equals(command)) {
			preUpdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("query".equals(command)) {
			query(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("notExam".equals(command)) {
			notExam(request, response);
		} else if ("examInfo".equals(command)) {
			examInfo(request, response);
		} else {
			throw new RuntimeException("请求失败" + command);
		}
	}


	/**
	 * 考试记录明细
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void examInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		IExamService examService = new ExamServiceImpl();
		// 获取条件查询条件
		String gradeName = request.getParameter("gradeName");
		if (gradeName != null && !"".equals(gradeName)) {
			request.setAttribute("gradeName", gradeName);
		}
		PageModel pageModel = new PageModel();
		String pageNo = request.getParameter("currentPage");
		int currentPage = pageNo != null && !"".equals(pageNo)?Integer.parseInt(pageNo):1;
		pageModel.setCurrentPage(currentPage);
		
		PageModel examPage = examService.examInfo(pageModel, gradeName);
		
		if (examPage != null) {
			request.setAttribute("examPage", examPage);
			request.getRequestDispatcher("view/teacher/test-admin/exam-info.jsp").forward(request, response);
		} else {
			response.sendRedirect("ExamServlet?command=main");
		}
	}

	/**
	 * 查询未参考学生列表（成绩为0）
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void notExam(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		IExamService examService = new ExamServiceImpl();
		// 获取条件查询条件
		String gradeName = request.getParameter("gradeName");
		if (gradeName != null && !"".equals(gradeName)) {
			request.setAttribute("gradeName", gradeName);
		}
		PageModel pageModel = new PageModel();
		String pageNo = request.getParameter("currentPage");
		int currentPage = pageNo != null && !"".equals(pageNo)?Integer.parseInt(pageNo):1;
		pageModel.setCurrentPage(currentPage);
		
		PageModel studentPage = examService.notExam(pageModel, gradeName);
		
		if (studentPage != null) {
			request.setAttribute("studentPage", studentPage);
			request.getRequestDispatcher("view/teacher/test-admin/not-exam.jsp").forward(request, response);
		} else {
			response.sendRedirect("ExamServlet?command=main");
		}
	}

	/**
	 * 添加考试
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		int subjectId = Integer.parseInt(request.getParameter("examSubjectId"));
		String examTime = request.getParameter("examTime");
		int gradeId = Integer.parseInt(request.getParameter("examGradeId"));
		int paperId = Integer.parseInt(request.getParameter("examPaperId"));
		int userId = Integer.parseInt(request.getParameter("examUserId"));
		
		ExamEntity exam = new ExamEntity();
		exam.setSubjectId(subjectId);
		try {
			exam.setExamTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(examTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		exam.setGradeId(gradeId);
		exam.setUserId(userId);
		exam.setPaperId(paperId);
		
		IExamService examService = new ExamServiceImpl();
		int row = examService.add(exam);
		
		if (row == 1) {
			response.sendRedirect("ExamServlet?command=main");
		} else {
			response.sendRedirect("ExamServlet?command=main");
		}
		
	}

	/**
	 * 添加考试页面初始化
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		IExamService examService = new ExamServiceImpl();
		ITeacherService teacherService = new TeacherServiceImpl();
		
		List<Map<String, Object>> paperList = examService.getPaper();
		List<Map<String, Object>> subjectList = examService.getSubject();
		List<Map<String, Object>> userList = examService.getUserInfo();
		List<Map<String, Object>> gradeList = teacherService.selectGrade();
		
		request.setAttribute("paperList", paperList);
		request.setAttribute("subjectList", subjectList);
		request.setAttribute("userList", userList);
		request.setAttribute("gradeList", gradeList);
		if (paperList != null && subjectList != null && userList != null && gradeList!=null){
			request.getRequestDispatcher("view/teacher/test-admin/test-add.jsp").forward(request, response);
		} else {
			response.sendRedirect("ExamServlet?command=main");
		}
	}

	/**
	 * 修改考试信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {

		int examId = Integer.parseInt(request.getParameter("examId"));
		int subjectId = Integer.parseInt(request.getParameter("examSubjectId"));
		String examTime = request.getParameter("examTime");
		int gradeId = Integer.parseInt(request.getParameter("examGradeId"));
		int paperId = Integer.parseInt(request.getParameter("examPaperId"));
		int userId = Integer.parseInt(request.getParameter("examUserId"));
		
		ExamEntity exam = new ExamEntity();
		exam.setSubjectId(subjectId);
		try {
			exam.setExamTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(examTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		exam.setGradeId(gradeId);
		exam.setUserId(userId);
		exam.setPaperId(paperId);
		exam.setId(examId);
		
		IExamService examService = new ExamServiceImpl();
		
		int row = examService.update(exam);
		
		if(row == 1) {
			response.sendRedirect("ExamServlet?command=main");
		} else {
			response.sendRedirect("ExamServlet?command=main");
		}
	}

	/**
	 * 修改前查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String examId = request.getParameter("examId");
		
		IExamService examService = new ExamServiceImpl();
		ITeacherService teacherService = new TeacherServiceImpl();
		
		Map<String, Object> exam = examService.preUpdate(examId);
		List<Map<String, Object>> paperList = examService.getPaper();
		List<Map<String, Object>> subjectList = examService.getSubject();
		List<Map<String, Object>> userList = examService.getUserInfo();
		List<Map<String, Object>> gradeList = teacherService.selectGrade();
		
		request.setAttribute("paperList", paperList);
		request.setAttribute("subjectList", subjectList);
		request.setAttribute("userList", userList);
		request.setAttribute("gradeList", gradeList);
		if(exam != null) {
			request.setAttribute("exam", exam);
			request.getRequestDispatcher("view/teacher/test-admin/test-update.jsp").forward(request, response);
		} else {
			response.sendRedirect("ExamServlet?command=main");
		}
	}

	/**
	 * 删除考试信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String examId = request.getParameter("examId");
		
		IExamService examService = new ExamServiceImpl();
		int row = examService.delete(examId);
		
		if(row == 1) {
			response.sendRedirect("ExamServlet?command=main");
		} else {
			response.sendRedirect("ExamServlet?command=main");
		}
	}

	/**
	 * 考试管理初始化
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		IExamService examService = new ExamServiceImpl();
		ITeacherService teacherService = new TeacherServiceImpl();
		// 获取条件查询条件
		String gradeName = request.getParameter("gradeName");
		if (gradeName != null && !"".equals(gradeName)) {
			request.setAttribute("gradeName", gradeName);
		}
		
		PageModel pageModel = new PageModel();
		String pageNo = request.getParameter("currentPage");
		int currentPage = !"".equals(pageNo) && pageNo !=null?Integer.parseInt(pageNo):1;
		pageModel.setCurrentPage(currentPage);
		
		PageModel examPage = examService.getStudentPageByPageModel(pageModel, gradeName);
		List<Map<String, Object>> paperList = examService.getPaper();
		List<Map<String, Object>> subjectList = examService.getSubject();
		List<Map<String, Object>> userList = examService.getUserInfo();
		List<Map<String, Object>> gradeList = teacherService.selectGrade();
		
		request.setAttribute("examPage", examPage);
		request.setAttribute("paperList", paperList);
		request.setAttribute("subjectList", subjectList);
		request.setAttribute("userList", userList);
		request.setAttribute("gradeList", gradeList);
		request.getRequestDispatcher("view/teacher/test-admin/test-main.jsp")
				.forward(request, response);
	}

}
